package cn.jbolt.ec.order;


import cn.jbolt.base.JBoltBaseService;
import cn.jbolt.common.model.BecRetailer;
import cn.jbolt.common.model.BecRetailerOrder;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import java.util.List;

public class OrderService extends JBoltBaseService<BecRetailerOrder> {
    private BecRetailerOrder dao = new BecRetailerOrder().dao();
    @Override
    protected BecRetailerOrder dao() {
        return dao;
    }

    public static final OrderService me=new OrderService();

    protected List<BecRetailerOrder> get_orders(int retailer_id,int member_id,int status){
        Kv cond = Kv.by("retailer_id",retailer_id);
        if(member_id>0) cond.set("client_id",member_id);
        if(status>-1) cond.set("status",status);
        return getCommonList(cond);
    }

    /**
     * 通过order_id获取订单商品
     * @param order_id
     * @return
     */
    public List<Record> query_order_commodity(String order_id){
        Kv cond=Kv.by("order_id",order_id);
        Kv param=Kv.by("order_id",getCommonList("id",cond).get(0).getId());
        return dbTemplate("ec.mall.shop.order.query_order_commodity",param).find();
    }

    /**
     * 根据订单状态获取会员的订单信息
     * @param client_id
     * @param status
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public Page<BecRetailerOrder> member_order_info(Integer client_id, Integer status, Integer pageNumber, Integer pageSize){
        Kv cond=Kv.by("client_id",client_id).set("status",status);
        return paginate("id,consignee,address,mobile,order_time,amount_total,amount_paid,order_id"
                ,cond,"id","desc",pageNumber,pageSize,false,false);

    }

    public Page<BecRetailerOrder> get_by_status(int[] statuses,int type,int pageNumber,int pageSize,int client_id,int retailer_id){

        Kv cond = Kv.by("statuses",statuses);
        if(retailer_id>0) cond.set("retailer_id",retailer_id);
        if(client_id>0) cond.set("client_id",client_id);
        if(type>0) cond.set("type",type);
        return daoTemplate("ec.mall.shop.order.query_by_status",cond).paginate(pageNumber,pageSize);
    }


    /*************************************************************
     * 后台管理方法
     * @author heqingyu
     * ***********************************************************
     */


    /**
     * 后台获取order首页分页信息
     * @param pageSize
     * @param pageNum
     * @param keywords
     * @param retailerId
     * @param type
     * @return
     */
    public Page<BecRetailerOrder> get_retailer_order_page(Integer pageSize, Integer pageNum, String keywords, Integer retailerId
            , Integer type, Long on_time){
        Kv paras = get_order_Kv( keywords, retailerId, type, on_time);
        return paginateBySqlTemplate("ec.mall.manage.order.get_retailer_order_page", paras,pageNum,pageSize);


    }


    public Integer get_order_amount_total(String keywords,Integer retailerId,Integer type,Long on_time){
        Kv paras = get_order_Kv( keywords, retailerId, type, on_time);
        paras.set("columns","sum(amount_total)");
        return Db.template("ec.mall.manage.order.get_retailer_order_page",paras).queryInt();

    }

    public Integer get_order_amount_paid(String keywords,Integer retailerId,Integer type,Long on_time){
        Kv paras = get_order_Kv( keywords, retailerId, type, on_time);
        paras.set("columns","sum(amount_paid)");
        return Db.template("ec.mall.manage.order.get_retailer_order_page",paras).queryInt();

    }


    public Integer get_order_retailer_amount(String keywords,Integer retailerId,Integer type,Long on_time){
        Kv paras = get_order_Kv( keywords, retailerId, type, on_time);
        paras.set("columns","sum(retailer_amount)");
        return Db.template("ec.mall.manage.order.get_retailer_order_page",paras).queryInt();

    }

    public Kv get_order_Kv( String keywords,Integer retailerId,Integer type,Long on_time){
        Kv paras=Kv.by("table",table());
        paras.setIfNotBlank("keywords",keywords);
        paras.setIfNotNull("retailer_id",retailerId);
        paras.setIfNotNull("type",type);
        if ( on_time != null){
            Long now_time = System.currentTimeMillis();
            Long juge_time = now_time - on_time*3600*24*1000;
            paras.setIfNotNull("juge_time",juge_time);
        }
        return  paras;
    }





}
